package com.von.util;

import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.convert.Convert;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.read.metadata.ReadSheet;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.von.converter.LocalDateConverter;
import com.von.converter.LocalDateTimeConverter;
import com.von.converter.LocalTimeConverter;
import com.von.listener.ExcelListener;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author xiaosai
 * @version 1.0
 * @date 2021/7/12 15:44
 * @describe excel工具类
 * @className
 */
public class ExcelUtils {

    /**
     * 导入excel
     * @param file 文件
     * @param tClass excel实例
     * @param <T>
     * @return
     */
    public static <T> List<T> importExcel(MultipartFile file,Class<T> tClass){
        if(file==null||file.isEmpty()){
            return new ArrayList<T>();
        }
        ExcelListener excelListener = new ExcelListener();
        try {
            InputStream inputStream = file.getInputStream();
            ExcelReader build = EasyExcel.read(inputStream, tClass,excelListener).build();
            //只读sheet0
            ReadSheet readSheet = EasyExcel.readSheet(0).build();
            build.read(readSheet);
            build.finish();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return Convert.toList(tClass,excelListener.getList());
    }

    /**
     * 导出excel
     * @param response
     * @param tClass excel实例
     * @param list 数据集
     * @param fileName 文件名例如：xxxx.xlsx
     * @param sheetName sheet名称
     * @param <T>
     */
    public static <T> void exportExcel2LocalDateTime(HttpServletResponse response,Class<?> tClass,List<T> list,String fileName,String sheetName){
        OutputStream outputStream = getOutputStream(response, fileName);
        ExcelWriter excelWriter = EasyExcel.write(outputStream,tClass).registerConverter(new LocalDateTimeConverter()).build();
        WriteSheet writeSheet = EasyExcel.writerSheet(sheetName).build();
        excelWriter.write(list, writeSheet);
        excelWriter.finish();
    }

    /**
     * 导出excel
     * @param response
     * @param tClass excel实例
     * @param list 数据集
     * @param fileName 文件名例如：xxxx.xlsx
     * @param sheetName sheet名称
     * @param <T>
     */
    public static <T> void exportExcel2LocalDate(HttpServletResponse response,Class<T> tClass,List<T> list,String fileName,String sheetName){
        OutputStream outputStream = getOutputStream(response, fileName);
        ExcelWriter excelWriter = EasyExcel.write(outputStream, tClass).registerConverter(new LocalDateConverter()).build();
        WriteSheet writeSheet = EasyExcel.writerSheet(sheetName).build();
        excelWriter.write(list, writeSheet);
        excelWriter.finish();
    }

    /**
     * 导出excel
     * @param response
     * @param tClass excel实例
     * @param list 数据集
     * @param fileName 文件名例如：xxxx.xlsx
     * @param sheetName sheet名称
     * @param <T>
     */
    public static <T> void exportExcel2LocalTime(HttpServletResponse response,Class<T> tClass,List<T> list,String fileName,String sheetName){
        OutputStream outputStream = getOutputStream(response, fileName);
        ExcelWriter excelWriter = EasyExcel.write(outputStream, tClass).registerConverter(new LocalTimeConverter()).build();
        WriteSheet writeSheet = EasyExcel.writerSheet(sheetName).build();
        excelWriter.write(list, writeSheet);
        excelWriter.finish();
    }

    /**
     * 解析表头
     * @param head
     * @return
     */
    public static List<List<String>> head(String head){
        List<List<String>> list = new ArrayList<>();
        List<String> asList = Arrays.asList(head.split(","));
        for(String a:asList){
            List<String> column = new ArrayList<>();
            column.add(a);
            list.add(column);
        }
        return list;
    }

    /**
     * 导出时生成OutputStream
     * @param response
     * @param fileName 文件名例如：xxxx.xlsx
     * @return
     */
    private static OutputStream getOutputStream(HttpServletResponse response, String fileName) {
        //创建本地文件
//        File file = new File(fileName);
        try {
//            if (!file.exists() || file.isDirectory()) file.createNewFile();
            fileName = new String(fileName.getBytes(), "ISO-8859-1");
            response.setContentType("application/vnd.ms-excel");
            response.addHeader("Content-Disposition", "attachment;filename=" + fileName);
            return response.getOutputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}
